package com.xzh.api;

import com.xzh.model.search.AppointmentDao;
import com.xzh.service.AppointmentService;
import com.xzh.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/api/v1/appointments")
public class AppointmentApi {
    private AppointmentService appointmentService;
    @Autowired
    public void setAppointmentService(AppointmentService appointmentService) {
        this.appointmentService = appointmentService;
    }
    @GetMapping
    public Map<String, Object> getAppointments(
            @RequestParam(defaultValue = "1") int pageNo,
            @RequestParam(defaultValue = "10") int pageSize,
            Integer studentId,
            Integer courseId,
            Integer scheduleId,
            String appointmentStatus) {

        // 创建 AppointmentDao 对象用于查询条件
        AppointmentDao appointment = new AppointmentDao();
        appointment.setStudentId(studentId);
        appointment.setCourseId(courseId);
        appointment.setScheduleId(scheduleId);
        appointment.setAppointmentStatus(appointmentStatus);

        return appointmentService.getAppointmentsWithPagination(pageNo, pageSize, appointment);
    }



    @PostMapping("list")
    public ResponseEntity<JsonResult<?>> addList(@RequestBody AppointmentDao appointmentDao){
        JsonResult<?> result = appointmentService.addList(appointmentDao);
        return ResponseEntity.ok(result);
    }

    @PutMapping
    public ResponseEntity<JsonResult<?>> updateAppointment(@RequestBody AppointmentDao appointmentDao) {
        boolean result = appointmentService.update(appointmentDao);
        if(result){
            return ResponseEntity.ok(JsonResult.success("更新成功"));
        }else {
            return ResponseEntity.ok(JsonResult.fail("更新失败"));
        }
    }

    @PutMapping("/{id}/complete")
    public  ResponseEntity<JsonResult<?>> completeAppointment(@PathVariable("id") Integer id) {
        boolean result = appointmentService.complete(id);
        if(result){
            return ResponseEntity.ok(JsonResult.success("预约状态更新成功"));
        }else {
            return ResponseEntity.ok(JsonResult.fail("预约状态更新失败"));
        }
    }

    @PutMapping("/{id}/confirm")
    public ResponseEntity<JsonResult<?>> confirmAppointment(@PathVariable("id") Integer id) {
        boolean result = appointmentService.confirm(id);
        if(result){
            return ResponseEntity.ok(JsonResult.success("预约状态更新成功"));
        }else {
            return ResponseEntity.ok(JsonResult.fail("预约状态更新失败"));
        }
    }

    @PutMapping("/{id}/cancel")
    public ResponseEntity<JsonResult<?>> cancelAppointment(@PathVariable("id") Integer id) {
        boolean result = appointmentService.cancel(id);
        if(result){
            return ResponseEntity.ok(JsonResult.success("取消预约成功"));
        }else {
            return ResponseEntity.ok(JsonResult.fail("取消预约失败"));
        }
    }

    @PutMapping("/{id}/reorder")
    public ResponseEntity<JsonResult<?>> reorderAppointment(@PathVariable("id") Integer id) {
        boolean result = appointmentService.reorder(id);
        if(result){
            return ResponseEntity.ok(JsonResult.success("重新预约成功"));
        }else {
            return ResponseEntity.ok(JsonResult.fail("重新预约失败"));
        }
    }

}
